
package com.xgry.bpm.api.base.socketuser.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xgry.bpm.api.base.socketuser.entity.SocketUser;
import com.xgry.bpm.api.base.socketuser.service.SocketUserService;
import com.xgry.bpm.api.base.userInfo.entity.UserInfo;
import com.xgry.bpm.api.base.userInfo.service.UserInfoService;
import com.xgry.bpm.common.enums.SystemEnum;
import com.xgry.bpm.common.utils.Result;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

;


/**
 * <p>
    *  前端控制器
    * </p>
 *
 * @author adrian
 * @since 2020-07-08
 * @version v1.0
 */
@RestController
@RequestMapping("/socketUser")
public class SocketUserController {

    @Autowired
    private SocketUserService ocketUserService;

    @Autowired
    private SocketUserService socketUserService;
    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping(value = "/list.do",produces = "application/json; charset=utf-8")
    public Result list() {
        return Result.success(socketUserService.list());
    }


    /**
     * 根据用户名获取待办事件集合
     * @param foUser
     * @return
     */
    @RequestMapping(value = "/findByfoUser.do",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public String findByfoUserAndMessageStatus(int foUser) {
        JSONObject jsonObject = new JSONObject();
        try{
            List<SocketUser> socketUsers = socketUserService.findByfoUser(foUser);
            jsonObject.put("state","success");
            jsonObject.put("message", "success");
            jsonObject.put("data", socketUsers);
        }catch (Exception e){
            jsonObject.put("state","error");
            jsonObject.put("message", "系统异常，请检查系统日志");
        }
        return jsonObject.toString();
    }


    @ResponseBody
    @RequestMapping(value = "/insertMessage.do",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public String inserMessage ( SocketUser socketUser) {
        JSONObject jsonObject = new JSONObject();
        try{
            socketUserService.save(socketUser);
            jsonObject.put("message", "success");
            jsonObject.put("data", "消息发送成功");
        }catch (Exception e){
            jsonObject.put("state","error");
            jsonObject.put("message", "系统异常，请检查系统日志");
        }
        return jsonObject.toString();

    }


    @ResponseBody
    @RequestMapping(value = "/countMessageNumber.do",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public String countMessageNumber(HttpSession session) {
        JSONObject jsonObject = new JSONObject();
        UserInfo userInfo = (UserInfo) SecurityUtils.getSubject().getPrincipal();
        try {
            int count = socketUserService.countMessage(userInfo.getUserId());
            jsonObject.put("message", "success");
            jsonObject.put("data", count);
        }catch (Exception e){
            jsonObject.put("state","error");
            jsonObject.put("message", "系统异常，请检查系统日志");
        }
        return jsonObject.toString();
    }

    /**
     * 获取当前用户所有的消息列表
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findBySocketUser.do",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public String findBySocketUser(HttpSession session) {
        JSONObject jsonObject = new JSONObject();
        try{
            UserInfo userInfo = (UserInfo)session.getAttribute(SystemEnum.SESSION_USER.getValue());
            List<SocketUser> socketUsers = socketUserService.findByUser(userInfo.getUserId());
            jsonObject.put("state", "success");
            jsonObject.put("data", socketUsers);
        } catch (Exception e){
            jsonObject.put("state","error");
            jsonObject.put("message", "系统异常，请检查系统日志");
        }
        return jsonObject.toString();
    }

    /**
     * 未读消息
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/unreadMessage.do",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public String unreadMessage(HttpSession session) {
        JSONObject jsonObject = new JSONObject();
        try{
            UserInfo userInfo = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            List<SocketUser> socketUsers = socketUserService.unreadMessage(userInfo.getUserId());

            jsonObject.put("state", "success");
            jsonObject.put("data", socketUsers);
        }catch (Exception e){
            jsonObject.put("state","error");
            jsonObject.put("message", "系统异常，请检查系统日志");
        }

        return jsonObject.toString();
    }

    /**
     * 已读消息
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/readMessage.do",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public String readMessage(HttpSession session) {
        JSONObject jsonObject = new JSONObject();
        try{
            UserInfo userInfo = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            List<SocketUser> socketUsers = socketUserService.readMessage(userInfo.getUserId());

            jsonObject.put("state", "success");
            jsonObject.put("data", socketUsers);
        }catch (Exception e){
            jsonObject.put("state","error");
            jsonObject.put("message", "系统异常，请检查系统日志");
        }

        return jsonObject.toString();
    }

    /**
     * 修改消息状态
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateMessageState.do",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public String updateMessageState(int socketId) {
        JSONObject jsonObject = new JSONObject();
        try {
            SocketUser socketUsers = socketUserService.getById(socketId);
            socketUsers.setMessageStutas(2);
            socketUserService.updateById(socketUsers);
        }catch (Exception e){
            jsonObject.put("state","error");
            jsonObject.put("message", "系统异常，请检查系统日志");
        }

        return jsonObject.toString();
    }

    /**
     * 查询单条消息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findById.do",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public String finById(int socketId) {
        JSONObject jsonObject = new JSONObject();
        try {
            SocketUser socketUsers = socketUserService.getById(socketId);
            jsonObject.put("message", "success");
            jsonObject.put("data", socketUsers);
        }catch (Exception e){
            jsonObject.put("state","error");
            jsonObject.put("message", "系统异常，请检查系统日志");
        }
        return jsonObject.toString();
    }

    //删除信息
    @RequestMapping(value = "deleteMsg.do",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @Transactional
    public String deleteMsg(String ids){
        System.out.println(ids);
        List<SocketUser> list = new ArrayList<>();
        JSONObject jsonObject = new JSONObject();
        try {
            JSONArray jsonArray = JSONArray.parseArray(ids);
            if (jsonArray.size()>0){
                for (int i = 0;i<jsonArray.size();i++){
                    JSONObject json = jsonArray.getJSONObject(i);
                    SocketUser s = new SocketUser();
                    if (json.getString("messageStutas").equals("未读消息")){
                        s.setMessageStutas(1);
                    }else{
                        s.setMessageStutas(2);
                    }
                    s.setSocketId(json.getInteger("socketId"));
                    s.setCreateTime(json.getString("createTime"));
                    s.setSocketUser(json.getInteger("socketUser"));
                    s.setSocketfoUser(json.getInteger("socketfoUser"));
                    s.setSocketfoUserName(json.getString("socketfoUserName"));
                    s.setSocketUserName(json.getString("socketUserName"));
                    s.setMessageRemark(json.getString("messageRemark"));
                    list.add(s);
                }
            }
            boolean b = socketUserService.deleteSocketUsers(list);
            if (b){
                jsonObject.put("message", "成功");
            }else
            {
                jsonObject.put("message", "失败");
            }

        }catch (Exception e){
            jsonObject.put("state","error");
            jsonObject.put("message", "系统异常，请检查系统日志");
        }
        return jsonObject.toString();
    }
}
